#include "encodetool.h"

void EnCodeTool::EnCode(proto::NetMsg& netMsg, Buffer::ptr& buffer)
{
    std::string str;
    netMsg.SerializeToString(&str);
    buffer->WriteInt(str.size());
    buffer->Write(str.c_str(), str.size());
}

bool EnCodeTool::DeCode(Buffer::ptr& buffer, proto::NetMsg& netMsg)
{
    if(buffer->GetSize() <= 4)
    {
        return false;
    }
    int len = buffer->ReadInt();
    if(len == buffer->GetSize())
    {
        char buf[len];
        buffer->Read(buf, len);
        netMsg.ParseFromArray(buf, len);
    }
    return false;
}